package com.example.lagou_elasticsearch_homework.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ListSerializer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.lagou_elasticsearch_homework.es.dao.PositionRepositoryES;
import com.example.lagou_elasticsearch_homework.es.model.PositionES;
import com.example.lagou_elasticsearch_homework.mysql.dao.PositionRepositoryMysql;
import com.example.lagou_elasticsearch_homework.mysql.model.PositionMysql;
import com.example.lagou_elasticsearch_homework.service.PositionService;
import com.example.lagou_elasticsearch_homework.utils.Util;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class PositionServiceImpl implements PositionService {

    private Integer batchSize = 5;
    @Autowired
    private PositionRepositoryMysql positionRepositoryMysql;
    @Autowired
    private PositionRepositoryES positionRepositoryES;

    /**
     * 从mysql导入es
     */
    @Override
    public void importAll(){
        /**
         * 计算总个数
         */
        long count = positionRepositoryMysql.count();
        System.out.println(String.format("总共:%d条记录",count));
        int opTimes = (int) (count/batchSize);
        System.out.println(String.format("总共要%d次",opTimes));
        for(int i=0;i<opTimes+1;i++){
            /**
             * 从mysql中查出
             */
            System.out.println(String.format("第%d次执行",i+1));
            PageRequest pageRequest = PageRequest.of(i,batchSize);
            Page<PositionMysql> pageResult = positionRepositoryMysql.findAll(pageRequest);
            List<PositionMysql> content = pageResult.getContent();
            if(CollectionUtils.isEmpty(content)){
                continue;
            }
            /**
             * 保存到es中
             */
            List<PositionES> positionESList = Util.convertList(content, PositionES.class);
            Iterable<PositionES> positionES = positionRepositoryES.saveAll(positionESList);
            System.out.println(JSON.toJSONString(positionES, SerializerFeature.PrettyFormat));
        }
        System.out.println("导入完成");
    }


    @Override
    public List<PositionES> findPositions(String positionName, Integer pageNum, Integer pageSize){
        /**
         * 根据positionName查询
         */
        PageRequest pageRequest = PageRequest.of(pageNum-1,pageSize);
        Page<PositionES> pageResult = positionRepositoryES.search(QueryBuilders.matchPhraseQuery("positionName", positionName), pageRequest);
        List<PositionES> positionNameResult = pageResult.getContent();
        /**
         * 如果查询结果少于5个，查询补上
         */
        if(positionNameResult.size()<5){
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("positionAdvantage", "美女多"))
                    .should(QueryBuilders.matchQuery("positionName", "员工福利好"));
            Iterable<PositionES> iterable = positionRepositoryES.search(boolQueryBuilder);
            ArrayList<PositionES> positionAd = Lists.newArrayList(iterable);
            //补够5个
            int num = 5-positionNameResult.size();
            List<PositionES> positionAdvantageResult = positionAd.subList(0, num);
            positionNameResult = ListUtils.union(positionNameResult,positionAdvantageResult);
        };
        return positionNameResult;
    }
}
